
// pages/food/food.js
const api = require('../../utils/api');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: null,
    pageNum: 1,
    pageSize: 10,
    comments:null,
    like: true,
    searchCommentParams:{},
    commentContent:null,
    addCommentParams:{},
    foodId:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
        foodId: options.id
    });    
    const foodId = this.data.foodId;
    this.loadFoodDetail(foodId);
  },

  loadFoodDetail(id){
    api.food.getFoodDetail(id).then(res => {
        this.setData({
            detail: res.records || res.list || res
        });
    }).catch(err => {
        console.error('加载详情失败：',err);
    });
  },

  loadComments(id){
    console.log('获取评论的id是：', id);
    const {pageNum, pageSize, searchCommentParams} = this.data;
    
    searchCommentParams.targetType = 1;
    //searchCommentParams.targetId = id;
    api.comment.getCommentList(pageNum, pageSize, searchCommentParams).then(res => {
        this.setData({
            comments: res.records || res.list || res
        });
    }).catch(err => {
        console.error('加载评论失败:',err);
        wx.showToast({title: '获取评论失败'});
    });
  },

  getLike(){
    const like = this.data.like;
    console.log(like);
    this.setData({
        like: !like
    });
  },

  onCommentInput(e) {
    this.setData({
        commentContent: e.detail.value
    });
  },

  submitComment(){
    const {foodId,commentContent,addCommentParams} = this.data;
    addCommentParams.foodId = foodId;
    addCommentParams.targetType = 1;
    addCommentParams.content = commentContent;
    api.comment.addComment(addCommentParams).then(res =>{
        wx.showToast({
          title: '评论成功',
        })
    }).catch(err => {
        console.error('评论失败',err);
    })
  },

 




  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})